package com.khadi.testcase.view;

import info.clearthought.layout.TableLayout;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

import com.khadi.testcase.util.SessionUtil;
import com.khadi.testcase.util.UIHelper;
import com.khadi.testcase.view.model.CustomComboModel;
import com.khadi.testcase.view.workflow.DefineTestCasesTab;
import com.khadi.testcase.view.workflow.DefineWorkFlowTab;
import com.khadi.testcase.view.workflow.ExecuteTestCaseTab;
import com.khadi.testcase.view.workflow.HtmlComponentTab;

public class TestCaseGeneration implements IWindowContent, ActionListener,
		ITestObserver {

	private static String ACTION_NEXT = "Next>>";
	private JComboBox cbUrl;
	private JTextArea tfArea;
	private IWindowContent htmlComponentTab;
	private IWindowContent defineWorkFlowTab;
	private JTabbedPane tbTabbedPane;
	private DefineTestCasesTab defineTestCasesTab;

	@Override
	public Object getData() {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dss.testcase.view.IWindowContent#getView()
	 */
	@Override
	public Component getView() {

		double[] columns = new double[] { -1 };
		double[] rows = new double[] { -2, -1, -2 };

		TableLayout mainLayout = new TableLayout(columns, rows);
		mainLayout.setHGap(5);
		mainLayout.setVGap(5);

		JPanel mainPanel = new JPanel(mainLayout);

		columns = new double[] { -2, -1 };
		rows = new double[] { -2, -2 };
		TableLayout browseLayout = new TableLayout(columns, rows);
		browseLayout.setHGap(5);
		browseLayout.setVGap(5);

		JPanel browsePanel = new JPanel(browseLayout);

		browsePanel.setBorder(BorderFactory.createTitledBorder("Browser"));

		JLabel lbUrl = new JLabel("URL");

		cbUrl = new JComboBox(new CustomComboModel<String>());
		cbUrl.setEditable(true);

		cbUrl.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				CustomComboModel<String> model = (CustomComboModel<String>) cbUrl
						.getModel();
				String url = (String) cbUrl.getSelectedItem();
				url = url.trim();
				if (!model.isElementExists(url)) {
					model.addElement(url);
				}
			}
		});

		JButton btnNext = new JButton("Browse");
		btnNext.setActionCommand(ACTION_NEXT);
		btnNext.addActionListener(this);

		tfArea = new JTextArea();
		tfArea.setAutoscrolls(true);

		JScrollPane scrPanel = new JScrollPane(tfArea);

		JPanel questionMarkPanel = new JPanel();

		JLabel lbQuestionMark = new JLabel(UIHelper
				.getImageIcon(UIHelper.IMG_QUESTION_MARK));

		questionMarkPanel.setLayout(new BorderLayout(15, 15));
		questionMarkPanel.add(lbQuestionMark, BorderLayout.EAST);
		questionMarkPanel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JOptionPane.showMessageDialog(e.getComponent(),
						"HTML Source code is not yet added..");
			}
		});

		browsePanel.add(lbUrl, "0,0");
		browsePanel.add(cbUrl, "1,0");
		browsePanel.add(btnNext, "0,1");
		mainPanel.add(browsePanel, "0,0");
		mainPanel.add(getTabPanel(), "0,1");
		mainPanel.add(questionMarkPanel, "0,2");

		return mainPanel;
	}

	private Component getTabPanel() {
		tbTabbedPane = new JTabbedPane();
		tbTabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);

		htmlComponentTab = new HtmlComponentTab(this);
		tbTabbedPane.addTab("Find HTML Components", htmlComponentTab.getView());

		defineWorkFlowTab = new DefineWorkFlowTab(this);
		tbTabbedPane.addTab("Define Work flow", defineWorkFlowTab.getView());

		tbTabbedPane.addTab("Define Test Cases", new JPanel());
		tbTabbedPane.addTab("Execute Test Cases", new JPanel());

		return tbTabbedPane;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals(ACTION_NEXT)) {
			doStartSelenium();
		}
	}

	private void doStartSelenium() {
		WebDriver driver = new InternetExplorerDriver();
		// And now use this to visit Google
		driver.get((String) cbUrl.getSelectedItem());
		// driver.get("https://iataindia.org/Forms/Login.aspx");

		SessionUtil.getInstance().setSessionObject(SessionUtil.WEB_DRIVER,
				driver);
		tfArea.setText(driver.getPageSource());

	}

	@Override
	public void notify(IWindowContent windowContent) {
		if (windowContent instanceof HtmlComponentTab) {
			tbTabbedPane.setSelectedIndex(1);
			defineWorkFlowTab.postData(windowContent.getData());
		} else if (windowContent instanceof DefineWorkFlowTab) {
			tbTabbedPane.setSelectedIndex(2);
			defineTestCasesTab = new DefineTestCasesTab(this);
			defineTestCasesTab.preData(defineWorkFlowTab.getData());
			tbTabbedPane.setComponentAt(2, defineTestCasesTab.getView());
			defineTestCasesTab.postData(null);
		} else if (windowContent instanceof DefineTestCasesTab) {
			tbTabbedPane.setSelectedIndex(3);
			ExecuteTestCaseTab executeTestCaseTab = new ExecuteTestCaseTab();
			executeTestCaseTab.preData(defineTestCasesTab.getData());
			tbTabbedPane.setComponentAt(3, executeTestCaseTab.getView());
			executeTestCaseTab.postData(null);
		}
	}

	@Override
	public void postData(Object object) {
		// TODO Auto-generated method stub

	}

	@Override
	public void preData(Object object) {
		// TODO Auto-generated method stub

	}
}
